import axios, { type AxiosResponse } from "axios";
import { ElMessage, ElMessageBox } from "element-plus";
import pinia from "@/stores/index";
import { useUserInfoStore } from "../stores/userInfo";

/* 定义response对象的data接口 */
interface ResponseData<T> {
  code: number;
  data: T;
  message: string;
  msg: any;
}


export const api = axios.create({
  timeout: 10000,
  headers: {
    
  }
});

// 配置新建一个 axios 实例
const service = axios.create({
  baseURL: import.meta.env.VITE_API_URL,
  timeout: 50000,
});

// 添加请求拦截器
service.interceptors.request.use((config: any) => {
  const userStore = useUserInfoStore(pinia);
  // console.log('admintokrn',userStore.token);
  
  if (userStore.token) {
    config.headers.admintoken = userStore.token;
  }
  return config;
});

// 添加响应拦截器
service.interceptors.response.use(
  /* 约束一下response */
  async (response: any) => {
    // 对响应数据做点什么
    const res = response.data;
    if (res.hasOwnProperty("code")) {
      if (res.code !== 0) {
        /* 成功数据的code值为20000/200 */
        // 统一的错误提示
        ElMessage({
          message:
            (typeof res.data == "string" && res.data) || res.msg || "Error",
          type: "error",
          duration: 5 * 1000,
        });

        //token值判断
        if (res.code == 100102 || res.code == 100101) {
          // const storeUserInfo = useUserInfoStore(pinia);
          // await storeUserInfo.reset();
          window.location.href = "/";
          ElMessageBox.alert("你已被登出，请重新登录", "提示", {})
            .then(() => {})
            .catch(() => {});
        }
        return Promise.reject(service.interceptors.response);
      } else {
        return res.data; /* 返回成功响应数据中的data属性数据 */
      }
    } else {
      return res;
    }
  },
  (error) => {
    // 对响应错误做点什么
    if (error.message.indexOf("timeout") != -1) {
      ElMessage.error("网络超时");
    } else if (error.message == "Network Error") {
      ElMessage.error("网络连接错误");
    } else {
      if (error.response.data) ElMessage.error(error.response.statusText);
      else ElMessage.error("接口路径找不到");
    }
    return Promise.reject(error);
  }
);

export default service;
